package cn.yuan.pet.controller;

import cn.yuan.basic.Exception.BusinessException;
import cn.yuan.basic.util.LoginContext;
import cn.yuan.basic.util.PageList;
import cn.yuan.pet.domain.Pet;
import cn.yuan.pet.domain.SearchMasterMsg;
import cn.yuan.pet.query.SearchMasterMsgQuery;
import cn.yuan.pet.service.ISearchMasterMsgService;
import cn.yuan.user.domain.Logininfo;
import cn.yuan.util.AjaxResult.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;


    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return searchMasterMsgService.loadAll();
    }

    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg seachMasterMsg , HttpServletRequest request) {
        try {
            Logininfo loginInfo = LoginContext.getLoginInfo(request);
            searchMasterMsgService.publish(seachMasterMsg,loginInfo.getId());
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("发布失败");
        }
    }
    //查询待处理消息  平台管理员和 店铺人员（管理员和员工）
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getLoginInfo(request);//为了把接口都放在这一个Controller中
            return searchMasterMsgService.pending(query,logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //查询已处理消息
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getLoginInfo(request);//为了把接口都放在这一个Controller中
            return searchMasterMsgService.processed(query,logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    @PostMapping("/list")
    public PageList<SearchMasterMsg> myMsg(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        try {
            Logininfo logininfo = LoginContext.getLoginInfo(request);//为了把接口都放在这一个Controller中
            return searchMasterMsgService.myMsg(query, logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    @PostMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet,HttpServletRequest request){
        try {
            Logininfo loginInfo = LoginContext.getLoginInfo(request);
            searchMasterMsgService.handle(pet,loginInfo.getId());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("处理失败!"+e.getMessage());
        }
    }
}
